package cn.schoolwow.quickdao.module.database.ddl.query.flow.index.getSingleEntityIndexFlow;

import cn.schoolwow.quickdao.annotation.IndexType;
import cn.schoolwow.quickdao.domain.database.ddl.IndexField;
import cn.schoolwow.quickdao.module.database.parent.domain.ResultSetConsumer;
import cn.schoolwow.quickdao.module.database.parent.flow.executor.ExecuteQueryConnectionFlow;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class GetSingleEntityIndexBySQLiteFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        String tableName = (String) flowContext.checkData("tableName");
        String indexName = (String) flowContext.checkData("indexName");
        flowContext.startFlow(new ExecuteQueryConnectionFlow())
                .putTemporaryData("sql", "select tbl_name, sql from sqlite_master where type='index' and tbl_name = ? and sql is not null;")
                .putTemporaryData("parameters", Arrays.asList(tableName))
                .putReturnData("resultSetConsumer",new ResultSetConsumer() {
                    @Override
                    public void consumeResultSet(ResultSet resultSet) throws Exception {
                        List<String> warningSQLList = new ArrayList<>();
                        while (resultSet.next()) {
                            String sql = resultSet.getString("sql");
                            if (!sql.contains("\"") && !sql.contains("`")) {
                                warningSQLList.add(sql);
                                continue;
                            }
                            String[] tokens = sql.split("[\"|`]");
                            IndexField indexField = new IndexField();
                            if (tokens[0].contains("UNIQUE")) {
                                indexField.indexType = IndexType.UNIQUE;
                            } else {
                                indexField.indexType = IndexType.NORMAL;
                            }
                            indexField.indexName = tokens[1];
                            indexField.tableName = tokens[3];
                            for (int i = 5; i < tokens.length - 1; i++) {
                                indexField.columns.add(tokens[i]);
                            }
                            if(indexField.indexName.equalsIgnoreCase(indexName)){
                                flowContext.putData("indexField", indexField);
                                break;
                            }
                        }
                        if(!warningSQLList.isEmpty()){
                            flowContext.putTemporaryData("warningSQLList", warningSQLList);
                            flowContext.remark("存在异常索引!索引不包含\"也不包含`");
                        }
                    }
                })
                .execute();
    }

    @Override
    public String name() {
        return "获取数据库单个表的索引信息-SQLite";
    }
}
